/**********************************************************
    注意本模块对INT16支持有限
**********************************************************/
#include "cmodel.h"

#ifndef USE_INT16
void load_feature_map_from_file(featureMap *img, char *filename)
{
    FILE *fp = fopen(filename, "r");
    fscanf(fp, "%d,%d,%d", &(img->channels), &(img->width), &(img->height));
    int len = img->channels * img->width * img->height;
    img->data = (DTYPE *)MEM_ALLOC(len * sizeof(DTYPE));
    for (int i = 0; i < len; i++)
    {
        fscanf(fp, "%f", img->data + i);
    }
}

void load_kernel_from_file(kernel *ker, char *filename)
{
    FILE *fp = fopen(filename, "r");
    fscanf(fp, "%d,%d,%d,%d", &(ker->out_channels), &(ker->in_channels), &(ker->width), &(ker->height));
    int len = ker->out_channels * ker->in_channels * ker->width * ker->height;
    ker->data = (DTYPE *)MEM_ALLOC(len * sizeof(DTYPE));
    for (int i = 0; i < len; i++)
    {
        fscanf(fp, "%f", ker->data + i);
    }
}

void load_bias_from_file(bias *b, char *filename)
{
    FILE *fp = fopen(filename, "r");
    fscanf(fp, "%d,", &(b->channels));
    int len = b->channels;
    b->data = (DTYPE *)MEM_ALLOC(len * sizeof(DTYPE));
    for (int i = 0; i < len; i++)
    {
        fscanf(fp, "%f", b->data + i);
    }
}
#endif // USE_INT16

void save_feature_map_to_file(featureMap img, char *filename)
{
    FILE *fp = fopen(filename, "w");
    fprintf(fp, "%d,%d,%d\n", img.channels, img.width, img.height);
    int len = img.channels * img.width * img.height;
    for (int i = 0; i < len; i++)
    {
        #ifdef USE_INT16
        fprintf(fp, "%d\n", img.data[i]);
        #else
        fprintf(fp, "%f\n", img.data[i]);
        #endif
    }
}

void save_kernel_to_file(kernel ker, char *filename)
{
    FILE *fp = fopen(filename, "w");
    fprintf(fp, "%d,%d,%d,%d\n", ker.in_channels, ker.out_channels, ker.width, ker.height);
    int len = ker.in_channels * ker.out_channels * ker.width * ker.height;
    for (int i = 0; i < len; i++)
    {
        #ifdef USE_INT16
        fprintf(fp, "%d\n", ker.data[i]);
        #else
        fprintf(fp, "%f\n", ker.data[i]);
        #endif
    }
}
